package learn;
import java.io.File;
import java.io.FilenameFilter;

/**
 * @Classname FileLearn
 * @Description TODO
 * @Date 2021/7/19 14:08
 * @Created by Alberthch
 */
public class FileLearn {
    public static class Filter implements FilenameFilter {

        String extent;

        Filter(String extent){
            this.extent=extent;
        }

        @Override
        public boolean accept(File dir, String name) {
            return name.contains('.'+extent);
        }
    }
    public static void main(String[] args) {
        File dir = new File("C:\\Users\\Alberthch\\Desktop\\算法笔记");
        System.out.println("所有文件及目录：");
        String[] fileNames = dir.list();
        for(String fileName:fileNames) {
            System.out.println(fileName);
        }

        System.out.println("\n加上过滤器，过滤python文件：");
        Filter filter = new Filter("py");
        String[] fileNamesByFilted = dir.list(filter);
        for(String fileName:fileNamesByFilted) {
            System.out.println(fileName);
        }
    }
}
